/*    */
package com.ryze.common.dubbo.project;


import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.StringUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;


public class ProjectEnvUtils {

    private static final Logger logger = LoggerFactory.getLogger(ProjectEnvUtils.class);

    //稳定项目环境group
    public static String STABLE_ENV = "project_stable";

    private static boolean INIT = false;

    private static String group = null;

    public synchronized static String getGroup(){
        if(!INIT){
            loadGlobalTestConfig();
        }
        return group;
    }


    public static boolean isProjectEnv(){
        if(StringUtils.isBlank(getGroup())){
            return false;
        }
        return true;
    }

    private static void loadGlobalTestConfig() {
        InputStream ins = null;
        Properties p = new Properties();
        try {
            File projectProperties = new File(new File(System.getProperty("user.home")), "dubbo-project.properties");
            if (projectProperties.exists()) {
                ins = new FileInputStream(projectProperties);
                p.load(ins);
                group = p.getProperty("projectGroup");
                logger.info("project env, group: "+ group);
            }
        } catch (Exception e) {
            logger.warn("loading dubbo-project.properties failed");
        } finally {
            try {
                if (ins != null) {
                    ins.close();
                }
            } catch (Exception ie) {
                logger.error(ie);
            }
        }
        INIT = true;
    }
}
